<template>
	<div>
		<div class="order-line margin">
			<span class="fl">请输入评价截至天数</span>
			<span class="fr">
				<input class="c-input" v-model="autoDay">
			</span>
		</div>
		<div class="order-line margin border select-container">
			<span class="fl">任务分配给</span>
			<select class="fr"
			:class="{empty: !selected}"
			v-model="selected">
				<option v-for="s in staff"
				:value="s.id"
				v-show="s.id">{{s.name}}</option>
			</select>
		</div>
		<div class="order-btn-container margin">
			<div class="c-btn big black" @click="popshow = true">退回分诊台</div>
			<div class="c-btn big reverse"
			:class="{disabled: !selected || loading}" @click="send">确认分配</div>
		</div>
		<pop :popshow="popshow"
		:popstyle="{width:'8.5rem'}">
			<txtpop title="取消原因"
			:maxlength="200"
			placeholder="请输入取消原因"
			@cancel="popshow = false"
			@send="cancel"></txtpop>
		</pop>
	</div>
</template>
<script>
import {myHttp, myAlert, nextPage} from '../assets/js/utils.js'
import {getUser, getDept} from '../assets/js/global.js'
import {tip} from '../assets/js/tip.js'
import pop from '../Pop.vue'
import txtpop from '../TxtPop.vue'
export default{
	name: 'Boss',
	mounted(){
		getDept().then((r) => {
			r[this.org].staff.forEach((e) => {
				this.staff.push(e);
			})
		})
	},
	data(){
		return{
			staff:[{
				id:'',
				name:'请选择维修人员'
			}],
			selected:'',
			popshow: false,
			autoDay: localStorage.getItem('autoDay'),
			loading: false
		}
	},
	methods:{
		send(){
			if(!this.selected || this.loading) return;
			if(this.autoDay < localStorage.getItem('autoDay')){
				myAlert.small(`自动评价天数不可少于${localStorage.getItem('autoDay')}天`)
				return;
			}
			myHttp('setRepairMaintain.do',{
				repairId: this.$route.params.id,
				operateUserId: localStorage.getItem('userId'),
				userId: this.selected,
				autoDay: this.autoDay
			},() => {
				this.loading = true;
			},{tip: true}).then((r) => {
				if(r.failed) {
					this.loading = false;
					return;
				}
				myAlert.small(tip.BOSS_SEND).then(() => {
					if(history.length == 1) this.$router.push({path:'/repair/list/boss'});
					else nextPage(this.$router, this.$route);
				})
			})
		},
		cancel(txt){
			if(this.loading) return;
			if(!txt) {
				myAlert.small('请输入取消原因');
				return;
			}
			myHttp('backToDispatch.do',{
				repairId: this.$route.params.id,
				operateUserId: localStorage.getItem('userId'),
				backReason: txt
			},() => {
				this.loading = true;
			}, {tip: true}).then((r) => {
				if(r.failed) {
					this.loading = false;
					return;
				}
				this.popshow = false;
				myAlert.small(tip.BOSS_BACK).then(() => {
					if(history.length == 1) this.$router.push({path:'/repair/list/boss'});
					else nextPage(this.$router, this.$route);
				})
			})
		}
	},
	props:['org'],
	components: {pop, txtpop}
}
</script>